package cn.itcast.weatherapp123;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.itcast.weatherapp123.adapter.FutureWeatherAdapter;
import cn.itcast.weatherapp123.adapter.HoureAdapter;
import cn.itcast.weatherapp123.bend.DayWeatherBend;
import cn.itcast.weatherapp123.bend.HoursBean;
import cn.itcast.weatherapp123.bend.OtherTipsBean;
import cn.itcast.weatherapp123.bend.WeatherBend;
import cn.itcast.weatherapp123.util.NetUtil;

public class MainActivity extends AppCompatActivity {

     private Button btn_Search;
     private TextView tv_city;
     private AppCompatSpinner sp_city;
     private String[] mCities;
     private EditText et_city;
     private TextView tvWeather, tvTem, tvTemLowHigh, tvWin, tvAir;
     private ImageView ivWeather;
     private RecyclerView rlv_future_weather,rlv_hours_weather;
     DayWeatherBend dayWeatherBend1;
     String EtCity="";
    //https://yiketianqi.com/api?unescape=1&version=v1&appid=84254468&appsecret=F1VYQPFH

    final public static String GAT="MainActivity";

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (msg.what==0){
                String weather=(String) msg.obj;
                Log.e(GAT, "------主线程收到了天气数据-weather-------" + weather);
                Gson gson=new Gson();
                WeatherBend weatherBend=gson.fromJson(weather,WeatherBend.class);
                tv_city.setText(weatherBend.getCity()+"市");
                Log.e("fan", "--解析后的数据-weather----" + weatherBend.toString());

                updateUiOfWeather(weatherBend);
            }

        }

    };

    private void updateUiOfWeather(WeatherBend weatherBend) {
        if (weatherBend!=null){

            final List<DayWeatherBend> dayWeatherBend=weatherBend.getDayWeathers();
            dayWeatherBend1=dayWeatherBend.get(0);

            tvTem.setText(dayWeatherBend1.getTem());
            tvWeather.setText(dayWeatherBend1.getWea()+"("+dayWeatherBend1.getDate()+dayWeatherBend1.getWeek()+")");
            tvTemLowHigh.setText(dayWeatherBend1.getTem2() + "-" + dayWeatherBend1.getTem1());
            tvWin.setText(dayWeatherBend1.getWin()[0] + dayWeatherBend1.getWinSpeed());
            tvAir.setText("空气：" + dayWeatherBend1.getAir() + dayWeatherBend1.getAirLevel() + "\n" + dayWeatherBend1.getAirTips()+"\n"+"(点击查询)");
            ivWeather.setImageResource(getImResOfWeather(dayWeatherBend1.getWeaImg()));


           dayWeatherBend.remove(0);


            FutureWeatherAdapter futureWeatherAdapter=new FutureWeatherAdapter(this,dayWeatherBend);
            rlv_future_weather.setAdapter(futureWeatherAdapter);
//            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false);
//            rlv_future_weather.setLayoutManager(linearLayoutManager);
            GridLayoutManager gridLayoutManager=new GridLayoutManager(this, 3, RecyclerView.VERTICAL, false);
            rlv_future_weather.setLayoutManager(gridLayoutManager);

            tvAir.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent=new Intent(MainActivity.this,TipsActivity.class);
                    intent.putExtra("tips", dayWeatherBend1);
                    startActivity(intent);

                }
            });

            List<HoursBean> hoursBean=dayWeatherBend1.getmHoursBean();
            HoureAdapter houreAdapter=new HoureAdapter(this,hoursBean);
            rlv_hours_weather.setAdapter(houreAdapter);

            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false);
            rlv_hours_weather.setLayoutManager(linearLayoutManager);
        }

    }

    private int getImResOfWeather(String weaImg) {

        int result = 0;
        switch (weaImg) {
            case "qing":
                result = R.mipmap.qing;
                break;
            case "yin":
                result = R.mipmap.yin;
                break;
            case "yu":
                result = R.mipmap.yu;
                break;
            case "bingbao":
                result = R.mipmap.bingbao;
                break;
            case "wu":
                result = R.mipmap.wu;
                break;
            case "shachen":
                result = R.mipmap.shachen;
                break;
            case "lei":
                result = R.mipmap.lei;
                break;
            case "xue":
                result = R.mipmap.xue;
                break;
            case "yun":
                result=R.mipmap.yun;
                break;
        }
        return result;
    }


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        //去除标题导航栏
        getSupportActionBar().hide();
        //去除时间和电量等
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tv_city=(TextView)findViewById(R.id.tv_city);
        SharedPreferences sharedPreferences=getSharedPreferences("weather_city",MODE_PRIVATE);
        EtCity=sharedPreferences.getString("city","北京市");
        requestHttp();



//        sp_city=(AppCompatSpinner)findViewById(R.id.sp_city);
//        mCities=getResources().getStringArray(R.array.cities);
//        ArrayAdapter adapter=new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,mCities);
//        sp_city.setAdapter(adapter);
//        sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
//
//                     String selectedcity=mCities[position];
//                     getWeatherOfCity(selectedcity);
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> adapterView) {
//
//            }
//        });

        tvWeather=findViewById(R.id.tv_weather);
        tvAir=findViewById(R.id.tv_air);
        tvTem=findViewById(R.id.tv_tem);
        tvTemLowHigh=findViewById(R.id.tv_tem_low_high);
        tvWin=findViewById(R.id.tv_win);
        et_city=(EditText)findViewById(R.id.et_city);
        btn_Search=(Button)findViewById(R.id.btn_Search);
        ivWeather=findViewById(R.id.iv_weather);

        rlv_future_weather=(RecyclerView)findViewById(R.id.rlv_future_weather);

        rlv_hours_weather=(RecyclerView)findViewById(R.id.rlv_hours_weather);



        btn_Search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  EtCity=et_city.getText().toString();


                SharedPreferences sharedPreferences=getSharedPreferences("weather_city",MODE_PRIVATE);
                SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putString("city",EtCity);
                editor.commit();
                requestHttp();

            }
        });


    }

    private void requestHttp() {
        new Thread(){
            @Override
            public void run() {

                super.run();

                String result=NetUtil.getweatherOfCity(EtCity);
                Message message=Message.obtain();
                message.what=0;
                message.obj=result;
                handler.sendMessage(message);

            }
        }.start();




    }


//    private void getWeatherOfCity(final String selectedcity) {
//
//        new Thread(){
//            @Override
//            public void run() {
//
//                super.run();
//
//                String result=NetUtil.getweatherOfCity(selectedcity);
//                Message message=Message.obtain();
//                message.what=0;
//                message.obj=result;
//                handler.sendMessage(message);
//
//            }
//        }.start();
//
//
//
//    }
}
